﻿/**
 * Copyright (C) 2013 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace NGuice.Inject.Internal
{
    public class UniqueAnnotations
    {
        private UniqueAnnotations() { }
        private static int nextUniqueValue = 1;

        /**
   * Returns an annotation instance that is not equal to any other annotation
   * instances, for use in creating distinct {@link com.google.inject.Key}s.
   */
        public static Attribute Create()
        {
            return Create(Interlocked.Increment(ref nextUniqueValue));
        }

        internal static Attribute Create(int value)
        {
            return new ValueInternalAttribute(value);
        }
    }

    [AttributeUsage(AttributeTargets.Method)]
    [BindingAnnotation]
    internal class InternalAttribute : Attribute
    {
        public virtual int Value
        {
            get;
            set;
        }
    }

    internal class ValueInternalAttribute : InternalAttribute
    {
        private int value;

        public ValueInternalAttribute(int value)
        {
            this.value = value;
        }

        public override int Value
        {
            get
            {
                return this.value;
            }
            set
            {
                this.value = value;
            }
        }

        public override string ToString()
        {
            return "@" + typeof(InternalAttribute).Name + "(value=" + value + ")";
        }

        public override bool Equals(object o)
        {
            return o is InternalAttribute
              && ((InternalAttribute)o).Value == Value;
        }

        public override int GetHashCode()
        {
            return (127 * "value".GetHashCode()) ^ value;
        }

    }
}
